package be.castanea.client.view.login;

import be.castanea.client.CastaneaClient;
import be.castanea.client.input.FengAdapter;
import com.jme.image.Texture;
import com.jme.input.KeyInput;
import com.jme.input.MouseInput;
import com.jme.math.Vector3f;
import com.jme.renderer.Renderer;
import com.jme.scene.state.RenderState;
import com.jme.scene.state.TextureState;
import com.jme.system.DisplaySystem;
import com.jmex.game.state.BasicGameState;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.fenggui.Button;
import org.fenggui.Container;
import org.fenggui.Display;
import org.fenggui.FengGUI;
import org.fenggui.Label;
import org.fenggui.TextEditor;
import org.fenggui.background.PlainBackground;
import org.fenggui.composites.Window;
import org.fenggui.event.ButtonPressedEvent;
import org.fenggui.event.IButtonPressedListener;
import org.fenggui.layout.Alignment;
import org.fenggui.layout.RowLayout;
import org.fenggui.render.lwjgl.LWJGLBinding;
import org.fenggui.util.Color;

/**
 * Castanea
 * 2009
 * @author Geert van Leemputten, Steven Rymenans, Bart Van Hooydonck
 */
public class LoginState extends BasicGameState {

    private Display display;
    private FengAdapter input;
    private TextureState texture;
    private TextEditor txtUsername;
    private TextEditor txtPassword;
    private Button btnLogin;

    public LoginState() {
        super("LOGIN");

        initFix();
        initComponents();
        initListeners();

        setActive(true);
    }

    public String getUsername() {
        return txtUsername.getText();
    }

    public String getPassword() {
        return txtPassword.getText();
    }

    @Override
    public void setActive(boolean active) {
        super.setActive(active);
        if (active) {
            display.layout();
            MouseInput.get().setCursorVisible(true);
            btnLogin.setEnabled(true);
        } else {
            MouseInput.get().setCursorVisible(false);
            btnLogin.setEnabled(false);
        //MouseInput.get().removeListeners();
        //MouseInput.destroyIfInitalized();
        //KeyInput.destroyIfInitalized();
        }
    }

    private void initFix() {
        // bugfix
        Texture defTex = TextureState.getDefaultTexture().createSimpleClone();
        texture = DisplaySystem.getDisplaySystem().getRenderer().createTextureState();
        defTex.setScale(new Vector3f(1, 1, 1));
        texture.setTexture(defTex);
    }

    public void initComponents() {
        final int width = DisplaySystem.getDisplaySystem().getWidth();
        final int height = DisplaySystem.getDisplaySystem().getHeight();

        display = new Display(new LWJGLBinding());
        input = new FengAdapter(display);

        Window window = FengGUI.createWindow(display, false, false, false, true);
        window.setTitle("Login");
        window.setSize(width / 2, height / 3);
        //window.removeWidget(window.getTitleBar());
        window.getAppearance().add(new PlainBackground(Color.WHITE_HALF_OPAQUE));

        window.setX(width / 2 - window.getWidth() / 2);
        window.setY(height / 2 - window.getHeight() / 2);
        window.getContentContainer().setLayoutManager(new RowLayout(false));

        Container userPanel = FengGUI.createContainer(window.getContentContainer());
        userPanel.setLayoutManager(new RowLayout());
        Container passPanel = FengGUI.createContainer(window.getContentContainer());
        passPanel.setLayoutManager(new RowLayout());
        Container loginPanel = FengGUI.createContainer(window.getContentContainer());
        passPanel.setLayoutManager(new RowLayout());

        final Label lblUsername = FengGUI.createLabel(userPanel, "Username");
        lblUsername.getAppearance().setAlignment(Alignment.MIDDLE);
        txtUsername = FengGUI.createTextArea(userPanel);

        final Label lblPassword = FengGUI.createLabel(passPanel, "Password");
        lblPassword.getAppearance().setAlignment(Alignment.MIDDLE);
        txtPassword = FengGUI.createTextArea(passPanel);
        btnLogin = FengGUI.createButton(loginPanel, "Login");
    }

    private void initListeners() {
        btnLogin.addButtonPressedListener(new IButtonPressedListener() {

            public void buttonPressed(ButtonPressedEvent arg0) {
                try {
                    CastaneaClient.login();
                } catch (IOException ex) {
                    Logger.getLogger(LoginState.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        });
    }

    @Override
    public void update(float tpf) {
        input.update(tpf);
    }

    @Override
    public void render(float tpf) {
        for (RenderState r : Renderer.defaultStateList) {
            r.apply();
        }
        texture.apply();
        display.display();
    }

    @Override
    public void cleanup() {
        MouseInput.get().removeListeners();
        MouseInput.destroyIfInitalized();
        KeyInput.destroyIfInitalized();
    }
}
